package com.example.farmeri.presenter.message;

import android.util.Log;

import com.example.farmeri.data.DataParam;
import com.example.farmeri.http.API;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.pojo.Messages;
import com.example.farmeri.presenter.base.BasePresenter;
import com.example.farmeri.tools.ResponseUtils;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.FormBody;
import okhttp3.RequestBody;

public class MessageManagerPresenter extends BasePresenter<IMessageManagerContract> {

    public MessageManagerPresenter(IMessageManagerContract view) {
        super(view);
    }

    public void getMessages(String uid, int page, Integer limit) {
        RequestBody requestBody = new FormBody.Builder()
                .add("uid",uid)
                .add("page", String.valueOf(page))
                .add("limit", limit.toString())
                .build();
        RetrofitManager
                .getManager()
                .getRetrofit()
                .create(API.class)
                .getMessages(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<Messages>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils<Messages> messageResponseUtils) {
                        if (messageResponseUtils.getCode() == 200) {
                            getView().onMessageResult(messageResponseUtils.getData());
                        }else {
                            getView().onMessageError(messageResponseUtils.getMessage());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e(TAG, "onError: e="+e.getCause().toString());
                        getView().onMessageError("消息数据获取失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    public void getAllMessages(String uid, int page, Integer limit) {
        RequestBody requestBody = new FormBody.Builder()
                .add("uid",uid)
                .add("page", String.valueOf(page))
                .add("limit", limit.toString())
                .build();
        RetrofitManager
                .getManager()
                .getRetrofit()
                .create(API.class)
                .getAllMessages(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<Messages>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils<Messages> messageResponseUtils) {
                        if (messageResponseUtils.getCode() == 200) {
                            getView().onMessageResult(messageResponseUtils.getData());
                        }else {
                            getView().onMessageError(messageResponseUtils.getMessage());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e(TAG, "onError: e="+e.getCause().toString());
                        getView().onMessageError("消息数据获取失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /**
     * 减少信誉值
     * @param id
     */
    public void reduceCredit(String id) {
        RequestBody requestBody = new FormBody.Builder()
                .add("id", id)
                .build();
        RetrofitManager.getManager().getRetrofit()
                .create(API.class)
                .reduceCredit(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils responseUtils) {
                        if(responseUtils.getCode() == 200){
                            DataParam.getUserInfo().setCredit(DataParam.getUserInfo().getCredit()-0.1f);
                            getView().onCreditReduceSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().showMessage("更新信誉值失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void readMessage(String messageId) {
        RequestBody requestBody = new FormBody.Builder()
                .add("id", messageId)
                .build();
        RetrofitManager.getManager().getRetrofit()
                .create(API.class)
                .readMessage(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils responseUtils) {
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().showMessage("消息已读失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void deleteMessage(String selectId) {
        RequestBody requestBody = new FormBody.Builder()
                .add("id", selectId)
                .build();
        RetrofitManager.getManager().getRetrofit()
                .create(API.class)
                .deleteMessage(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils responseUtils) {
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().showMessage("消息删除失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
